<?php

if (@$_GET['q']) {
	
  require_once '/../lib/google-api/src/Google/Client.php';
  require_once '../lib/google-api/src/Google/autoload.php';
  require_once '/../lib/google-api/src/Google/Service/YouTube.php';

  $DEVELOPER_KEY = 'AIzaSyBKk5FIpKU3xSusvJZtsOylTXRFxmEV9nY';
  $client = new Google_Client();
  $client->setDeveloperKey($DEVELOPER_KEY);

  $youtube = new Google_Service_YouTube($client);
  try {
	  
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'] . " Gameplay",
      'maxResults' => 1,
    ));

    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $video = $searchResult['id']['videoId'];
          break;
      }
    }
   
  } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  }
}

echo "https://www.youtube.com/embed/" . $video;

?>